public class 递归 {
    public static int fib2(int i) {
        if (i == 1 || i == 2) {
            return 1;
        }
        int f1 = 1;
        int f2 = 1;
        int f3 = 0;
        for (int a = 3; a <= i; a++) {
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }
    public static void main(String[] args) {
        //斐波那契迭代
        System.out.println(fib2(3));

    }
    public static int fib(int i) {
        if ((i == 1) || (i == 2)) {
            return 1;
        }else {
            return fib(i - 1) + fib(i - 2);
        }
    }
    public static void main6(String[] args) {
        //斐波那契数列递归
        System.out.println(fib(3));

    }
    public static int add1(int a, int b) {
        return a + b;
    }
    public static float add1(float x, float y) {
        return x + y;
    }
    //重载
    public static void main5(String[] args) {

    }
    public static int add(int a) {
        if (a <= 9) {
            return a;
        }else {
            return a % 10 + add(a / 10);
        }
    }
    public static void main4(String[] args) {
        //给一个数打印加法
        System.out.println(add(1729));
    }
    public static int function(int i) {
        if (i == 1) {
            return 1;
        }else {
            return i + function(i - 1);
        }
    }
    public static void main3(String[] args) {
        //递归加法
        int b = function(10);
        System.out.println(b);
    }
    public static void func(int i) {
        if (i <= 9) {
            System.out.println(i);
        }else {
            func(i / 10);
            System.out.println(i % 10);
        }
    }
    public static void main2(String[] args) {
        //顺序打印
        func(1234);
    }
    //阶乘递归
    public static int fun(int i) {
        if (i == 1) {
            return 1;
        }else {
            return i * fun(i - 1);
        }

    }
    public static void main1(String[] args) {
        int a = fun(5);
        System.out.println(a);
    }
}
